#include <stdio.h>
#include <unistd.h>
#include "StdThread.h"
#include <pthread.h>


pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER; // 静态初始化



void *func(void * arg)
{
    int * count = (int*)arg;
    for (int idx = 0; idx < 5; idx++)
    {
        pthread_mutex_lock(&mutex);// 上锁
        (*count)++;
        pthread_mutex_unlock(&mutex);// 解锁
    }
}

void *func2(void * arg)
{
    int * count = (int*)arg;
    for (int idx = 0; idx < 3; idx++)
    {
        pthread_mutex_lock(&mutex);// 上锁
        (*count)++;
        pthread_mutex_unlock(&mutex);// 解锁
    }
}

int main()
{
    pthread_mutex_init(&mutex, NULL);// 动态初始化
    int count = 0;
    Thread *t1 = ThreadInit(func, &count);
    Thread *t2 = ThreadInit(func, &count);

    ThreadJoin(t1);
    ThreadJoin(t2);

    printf("count:%d\n", count);

    pthread_mutex_destroy(&mutex);// 销毁锁
    

    return 0;
}